<?php
class ImportUtXML {
    public function load($file) {
        $dom = new DOMDocument();
        $dom -> load($file);
        $xpath = new DOMXpath($dom);
        $list = $xpath -> query("//body/div1");
        $section_id = '';

        if ($list -> length > 0) {
            $div1 = $list -> item(0);
            $type = $div1 -> getAttribute('type');
            if ($type == 'section') {
                $section_id = $div1 -> getAttribute('xml:id');
            }
        }

        //import ut*.xml file
        $letter = D('Translation');
        $div2 = $xpath -> query("//div2[@type='translation']");
        foreach ($div2 as $elem) {
            $data = array();
            $data['section_id'] = $section_id;
            $data['translation_id'] = $elem -> getAttribute('xml:id');
            $data = $this -> readNode($elem, $data);
            $letter -> add($data);
        }
    }
 

    private function readNode(DOMElement $elem, $data) {
        $tagName = $elem -> tagName;

        $text = null;
        if ($tagName == 'name') {
            $type = $elem -> getAttribute('type');
            $text = $elem -> firstChild -> textContent;
            switch($type) {
                case 'absender' :
                    $i = strrpos($text, ' ');
                    if (!$i) {
                        $data['abs_firstname'] = $text;
                        break;
                    } else {
                        $data['abs_firstname'] = substr($text, 0, $i);
                        $data['abs_lastname'] = substr($text, $i + 1);
                    }

                    break;
                case 'adressat' :
                    $data['adr_firstname'] = $text;
                case 'place' :
                    if (empty($data['adr_firstname'])) {
                        $data['abs_ort'] = $text;
                    } else {
                        $data['adr_ort'] = $text;
                    }
                    break;
                case 'lastname' :
                    $parent = $elem -> parentNode;
                    if ($parent -> tagName === 'name') {
                        $parentType = $parent -> getAttribute('type');
                        if ($parentType == 'absender') {
                            $data['abs_lastname'] = $text;
                        } else if ($parentType == 'adressat') {
                            $data['adr_lastname'] = $text;
                        }
                    }
                    break;
            }
        } else if ($tagName == 'date') {
            $rend = $elem -> getAttribute('rend');
            $n = $elem -> getAttribute('n');
            switch($rend) {
                case 'datum' :
                    if (!empty($n)) {
                        $data['tag'] = substr($n, 0, 2);
                        $data['monat'] = substr($n, 2, 2);
                        $data['jahr'] = substr($n, 4);
                    }
                    break;
                case 'day' :
                    if (!empty($n)) {
                        $data['day_n'] = $n;
                    }
                    break;
            }
        } else if ($tagName == 'ref') {
            $type = $elem -> getAttribute('type');
            $text = $elem -> textContent;
            switch($type) {
                case 'kategorie' :
                    $data['kategorie_rend'] = $elem -> getAttribute('rend');
                    $data['kategorie'] = $text;
                    break;
                case 'faksimile' :
                    $data['faksimile'] = $text;
                    $data['faksimile_n'] = $elem -> getAttribute('n');
                    break;
                case 'bemerkung' :
                    $data['bemerkung'] = $text;
                    break;
                case 'status' :
                    $data['status'] = $text;
                    break;
                case 'prev' :
                    $data['prev'] = $elem -> getAttribute('target');
                    break;
                case 'next' :
                    $data['next'] = $elem -> getAttribute('target');
                    break;
            }
        } else if ($tagName == 'lb') {
            $n = $elem -> getAttribute('n');
            if (!empty($n)) {
                $data['lb_n'] = $n;
            }
        } else if ($tagName == 'label' && $elem -> getAttribute('rend') == 'briefnr') {
            $data['briefnr'] = $elem -> textContent;
        }

        $cList = $elem -> childNodes;
        foreach ($cList as $c) {
            if ($c -> nodeType == 1) {
                $data = $this -> readNode($c, $data);
            }
        }
        return $data;
    }

}
